var shopUnique;//商铺唯一性标识
var saleListUnique;//订单唯一性标识
var saleListTotal=0;//订单总价
var cusUnique;//用户唯一性标识
var goodsCount;//商品数量
var goodsIds;//商品条形码
var basketIds;//
var goodsPrice;//
var goodsUniques;//
var cusFlag;//客户身份 0-注册用户  1-游客

var imgUrl="http://121.42.189.191:80/";//全局变量

//初始化页面
$(document).ready(function() {
	var thisURL = document.URL;  
	var  getval =thisURL.split('?')[1].split('&'); 
	cusUnique=getval[0].split("=")[1];
	var totalCount=getCookie("totalCount");
	var dest=getval[2].split("=")[1];
	if(dest==1){
		loadAddressCus(cusUnique);
		loadCusInfo(cusUnique);//显示用户名
		
	}else if(dest==2){//从地址页面返回的话加载地址信息
		var addrId=getval[3].split("=")[1];
		loadAddressById(addrId);//加载地址信息
		loadCusInfo(cusUnique);//显示用户名
		
	}else if(dest==3){
		console.log("cusUnique:"+cusUnique+";dest:"+dest);
		loadCusInfo(cusUnique);//显示用户名
	}else if(dest==4){//直接购买
		loadAddressCus(cusUnique);
		loadCusInfo(cusUnique);//显示用户名
	}
	
	basketIds=getCookie("basketIds");//获取购物车ids
	
//	saleListTotal=getCookie("saleListTotal");
//	goodsCount=getCookie("goodsCount");
//	goodsIds=getCookie("goodsBarcode");
//	goodsPrice=getCookie("goodsPrice");
//	goodsUniques=getCookie("goodsUniques");
//	console.log("totals33SS:"+saleListTotal+";count:"+goodsCount+";barcode:"+goodsIds+"basketId"+basketIds+"goodsPrice:"+goodsPrice+"goodsUniques:"+goodsUniques);
//	$("#totalCount").text(totalCount);//商品数量
//	$("#totalPrice").text(saleListTotal);
	
	
	saleDetails(basketIds);//根据商品Id查询商品详情信息

});

//根据地址id查询地址信息
function loadAddressById(addrId){
	$.ajax({
		url : "../cus/findAddrByIdA.do",
		type : "post",
		data : {
			"addrId" : addrId
		},
		dataType : "json",
		success : function(result) {
			console.log(result);
			if (result.status == 1) {// 查询成功
				$("#addressName").text(result.data.addrReceiverName);
				$("#addrAddr").text(result.data.addrAddr);
				$("#addrPhone").text(result.data.addrPhone);
				addrId=result.data.addrId;
			} else {

			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
		}
	});
}





//加载用户基本信息
function loadCusInfo(cusUnique) {
	
	$.ajax({
		url : "../cus/findCusById.do",
		type : "post",
		data : {
			"cusUnique" : cusUnique
		},
		dataType : "json",
		success : function(result) {
			
			console.log(result);
			if (result.status == 1) {// 查询成功
				$("#cusName").text(result.data.cusAccount);
//				$("#cusPhone").text(result.data.cusPhone);
			} else {
				$("#cusName").text("游客");
				$("#cusPhone").text("");
				$("#cusPoints").text(0);
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
		}
	});
}


//加载用户地址信息
function loadAddressCus(cusUnique){
	console.log("加载地址");
	
	$.ajax({
		url : "../cus/findAddrById.do",
		type : "post",
		data : {"cusUnique":cusUnique},
		dataType : "json",
		success : function(result) {
			
			if (result.status == 1) {// 查询成功
				$("#addressName").text(result.data[0].addrReceiverName);
				$("#addrAddr").text(result.data[0].addrAddr);
				$("#addrPhone").text(result.data[0].addrPhone);
				$("#addrId").text(result.data[0].addrId);
			} else {
				
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {

		}
	});
}


/**
 * 动态查询用户购物车中的商品数量
 */
function sumBasket(shopUnique) {
	
	console.log(shopUnique+";"+cusUnique);
	$.ajax({
		url : "../basket/findSumBasket.do",
		type : "post",
		data : {
			"cusId" : cusUnique,"shopUnique":shopUnique
		},
		dataType : "json",
		success : function(result) {
			console.log(result);
			if (result.status == 1) {// 查询成功
				$("#shopcart-number").text(result.data);
			} else {
				$("#shopcart-number").text(0);
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {

		}
	});

}




/*
 * 根据商品id查询出对应的商品信息 goodId 商品的id
 */
function saleDetails(basketIds) {
	
	var after = basketIds.split(";");
	console.log(after);
	$.ajax({
		url : "../basket/findBasketByIds.do",
		type : "post",
		traditional : true,
		data : {
			"basketIds" : after
				},
				dataType : "json",
				success : function(result) {
					console.log(result);
					if (result.status == 1) {// 查询成功
						shopUnique = result.data[0].shop_unique;
						cusFlag = result.data[0].shopping_cart_flag;
						console.log(result.data);
						$("#saleList").empty();
						var data = result.data;
						var tbody = "";
						for (var i = 0; i < data.length; i++) {
							var tr = "";
							tr += '<div class="lie clearfloat basketDiv">'
									+ '<span class="basketId" style="display:none">'
									+ data[i].shopping_cart_id
									+ '</span>'
									+ '<span class="goodsId" style="display:none">'
									+ data[i].goods_barcode
									+ '</span>'
									
									+ '<span class="goodsName" style="display:none">'
									+ data[i].goods_name
									+ '</span>'
									
									
									+ '<span class="goodsUnique" style="display:none">'
									+ data[i].goods_unique
									+ '</span>'
									+ '<span class="shopUnique" style="display:none">'
									+ data[i].shop_unique
									+ '</span>'
									+ '<span class="basketPrice" style="display:none">'
									+ data[i].goods_sale_price
									+ '</span>'

									+ '<a href="">'
									+ '<div class="tu clearfloat fl">'
									+ '<img id="goods-pic" src="'+imgUrl+data[i].goods_picturepath+'"/>'
									+ '</div>'
									+ '</a>'
									+ '<div class="right clearfloat fl">'
									+ '<a href="detail.html">'
									+ '<p class="tit over" id="confirm-goodsName">'
									+ data[i].goods_name
									+ '</p>'
									+ '</a>'
									+ '<div class="xia clearfloat">'
									+ '<a href="detail.html">'
									+ '<p class="jifen fl over"><samp>￥</samp><samp>'
									+ data[i].goods_sale_price
									+ '</samp></p>'
									+ '<p class="jifen fl over"><samp>数量：</samp><samp class="basketTotal">'
									+ data[i].shopping_cart_count
									+ '</samp></p>'
									+ '</a>'
									+ '</div>'
									+ '</div>' + '</div>';
							tbody += tr;
						}
						$("#saleList").append(tbody);
						calculate();//计算商品总价格

						loadAddress(cusFlag);//加载地址信息
						sumBasket(shopUnique);//加载购物车信息 

					} else {
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {

				}
			});
}


// 加载地址
function loadAddress(cusFlag){
	
	if (cusFlag == 0) {
		$.ajax({
			url : "../cus/findAddrById.do",
			type : "post",
			data : {
				"cusUnique" : cusUnique
			},
			dataType : "json",
			success : function(result) {
				console.log(result);
				if (result.status == 1) {// 查询成功
					$("#saleName").text(result.data[0].addrReceiverName);
					$("#saleAddress").text(result.data[0].addrAddr);
					$("#salePhone").text(result.data[0].addrPhone);
					$("#addrId").text(result.data[0].addrId);
				} else {
					
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {

			}
		});

	} else {

	}

}

//跳转地址列表
function addAddress(){
	window.location.href="address.html?cusUnique="+cusUnique;
	
}

//跳转登录页面
function toLogin(){
	window.location.href="login.html?cusUnique="+cusUnique+"&dest="+1+"&end="+new Date().getTime();
	
}

// 数量增加
function confirmAdd(){
	var price =$("#confirm-goodsPrice").text();
	var count=$("#goodsCount").text();
	var totalPrice=$("#totalPrice").text();
	$("#goodsCount").text(++count);
	$("#totalCount").text(count);
	$("#totalPrice").text(count*price);
	
	console.log(price+";"+count+";"+totalPrice);
	
}

//数量减少
function confirmSub(){
	
	var price =$("#confirm-goodsPrice").text();
	var count=$("#goodsCount").text();
	var totalPrice=$("#totalPrice").text();
	if(count>1){
		$("#goodsCount").text(--count);
		$("#totalCount").text(count);
		$("#totalPrice").text(count*price);
	}
	
}


//跳转购物车页面
function goShopcar(){
	window.location.href="shopcar.html?cusUnique="+cusUnique+"&shopUnique="+shopUnique;
}

//去商品列表页面
function goList(){
	if(shopUnique!=null){
		window.location.href="list.html?cusUnique="+cusUnique+"&shopUnique="+shopUnique+"&cusFlag="+cusFlag;
	}
}

//计算总价格数量
function calculate(){
	var sums=0;
	var totalCount=0;
	$("#saleList").find("span.basketId").each(
			function() {
					var count =$(this).parent().find("samp.basketTotal").html();//获取数量
					var price = $(this).parent().find("span.basketPrice").text();//获取当前价格
					var sum=parseFloat(price*count);
					totalCount=totalCount+parseFloat(count);
					sums=sums+sum;
			});
	
	$("#totalCount").text(totalCount);//商品数量
	$("#totalPrice").text(sums);//商品总价格
	
}


//提交订单
function submitOrder() {
	var maySubmit = false;
	var price=$("#totalPrice").text();
	if(price<0.00001){
		maySubmit=true;
		return;
	}
	cusId ="001";
	var saleListAddress=$("#addressName").text()+";"+$("#addrAddr").text()+";"+$("#addrPhone").text();
	if($("#addressName").text()==""||$("#addrPhone").text()==""||$("#addrAddr").text()==""){
		alert("地址不能为空");
		return;
	}

	var confirmMsg=$("#confirmMsg").val();//买家留言信息
	if (!maySubmit) {
		
		var goodsIds = "";
		var goodsCount = "";
		var totalCount=0;
		var goodsPrice = "";
		var goodsUniques="";
		var goodsNames="";
		var after = new Array();
		var basketIds = "";
		var sums=0;
		$("#saleList").find("span.basketId").each(
				function() {
					
						var count =$(this).parent().find("samp.basketTotal").html();//获取数量
						var price = $(this).parent().find("span.basketPrice").text();//获取当前价格
						var goodsId = $(this).parent().find("span.goodsId").text();//获取当前商品条形码
						var basketId = $(this).text();//获取当前购物车id
						var goodsUnique = $(this).parent().find("span.goodsUnique").text();//获取当前价格;// 获取的是商品唯一性标识
						
						var goodsName=$(this).parent().find("span.goodsName").text();//获取商品名称
						
						var sum=parseFloat(price*count);
						saleListTotal=saleListTotal+sum;
						goodsCount = goodsCount + count + ";";
						totalCount=parseInt(totalCount)+parseInt(count);
						goodsPrice = goodsPrice + price + ";";
						goodsIds = goodsIds + goodsId + ";";
						basketIds = basketIds + basketId + ";";
						goodsUniques=goodsUniques+goodsUnique+";";
						
						goodsNames=goodsNames+goodsName+";";
						
						
						
						
				});
		
		addCookie("goodsBarcode", goodsIds, 1);// 存储小时
		addCookie("goodsCount", goodsCount, 1);// 存储小时
		addCookie("goodsPrice", goodsPrice, 1);// 存储小时
		addCookie("goodsUniques", goodsUniques, 1);// 存储小时
		addCookie("saleListTotal", saleListTotal, 1);// 订单总价存储小时
		addCookie("totalCount", totalCount, 1);// 商品数量存储小时
		
		console.log("totals:"+saleListTotal+"count:"+goodsCount+"barcode:"+goodsIds+"basketId:"+basketIds+"goodsPrice:"+goodsPrice+"goodsUniques:"+goodsUniques);
		console.log("shopUnique:"+shopUnique+"cusUnique:"+cusUnique+"saleListAddress:"+saleListAddress+"confirmMsg:"+confirmMsg);
		
		
		var after = new Array();
		after.push(goodsIds);
		after.push(goodsCount);
		after.push(goodsPrice);
		after.push(goodsUniques);
		after.push(basketIds);
		after.push(goodsNames);
		console.log(after);//

		$.ajax({
			url : "../online/addSale.do",
			type : "post",
			traditional : true,
			data : {
				"after" : after,
				"saleListTotal" : saleListTotal,"shopUnique":shopUnique,
				"cusUnique" : cusUnique,"saleListAddress":saleListAddress,"confirmMsg":confirmMsg
			},
			dataType : "json",
			success : function(result) {
				console.log(result);
				if (result.status == 1) {// 查询成功
					addCookie("saleListUnique", result.data, 0);// 存储小时
					window.location = "submit-order.html?saleListUnique="+result.data+"&saleListTotal="+saleListTotal ;


					//					window.location = "sale-order.html" ;
				} else {
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {

			}
		});

	}

}










